package com.cgtong.android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.cgtong.android.R;
import com.cgtong.android.bean.ActivityBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.widget.GlideApp;

import java.util.List;

/**
 * Created by Sky.Bing on 2017/10/18.
 */

public class ActivitiesAdapter extends RecyclerView.Adapter<ActivitiesAdapter.ActivitiesHolder> {
    private Context mContext;
    private List<ActivityBean> activityBeanList;
    private View.OnClickListener itemClickListener;

    public static enum ITEM_TYPE {  
        ITEM_TYPE_Left,  
        ITEM_TYPE_Right  
    }  

    public ActivitiesAdapter(Context context, List<ActivityBean> activityBeanList, View.OnClickListener onClickListener) {
        mContext = context;
        this.activityBeanList = activityBeanList;
        this.itemClickListener = onClickListener;
    }

    @Override
    public ActivitiesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ActivitiesHolder vh = null;
        if (viewType == ITEM_TYPE.ITEM_TYPE_Left.ordinal()){ 
          vh = new ActivitiesHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_home_activities, parent, false));
        }else if(viewType == ITEM_TYPE.ITEM_TYPE_Right.ordinal()){
          vh = new ActivitiesHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_home_activities_right, parent, false));
        }
        if(null != vh)
            vh.convertView.setOnClickListener(itemClickListener);
        return vh;
    }

    @Override
    public void onBindViewHolder(ActivitiesHolder holder, int position) {
        ActivityBean activityBean = activityBeanList.get(position);
        holder.actName.setText(activityBean.getTitle());
        holder.convertView.setTag(activityBean.getId());
        /*if (!TextUtils.isEmpty(activityBean.getContent())) {
            holder.actDescription.setText(activityBean.getContent());
        } else {
            holder.actDescription.setText("");
        }*/
        GlideApp.with(mContext).load(activityBean.getImg_url()).placeholder(R.drawable.icon_default_loading).error(R.drawable.icon_default_error).centerCrop().into(holder.actImage);
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.convertView.getLayoutParams();
        layoutParams.width = ScreenUtils.getScreenWidth() / 2;
        /*if (position % 2 == 0) {
            holder.actName.setTextColor(mContext.getResources().getColor(R.color.color_txt_activity_name));
            holder.actDescription.setTextColor(mContext.getResources().getColor(R.color.color_txt_activity_name));
            holder.convertView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.bg_activities_left));
        } else {
            holder.convertView.setBackgroundColor(mContext.getResources().getColor(R.color.color_bg_home_activities_right));
        }*/
    }

    public int getItemViewType(int position){  
        return position % 2 == 0 ? ITEM_TYPE.ITEM_TYPE_Left.ordinal() : ITEM_TYPE.ITEM_TYPE_Right.ordinal();  
    }

    @Override
    public int getItemCount() {
        return activityBeanList.size() > ConstantManager.HOME_MAX_ACTIVITIES ? ConstantManager.HOME_MAX_ACTIVITIES : activityBeanList.size();
    }


    class ActivitiesHolder extends RecyclerView.ViewHolder {
        public View convertView;
        public TextView actName;
        //public TextView actDescription;
        public ImageView actImage;

        public ActivitiesHolder(View itemView) {
            super(itemView);
            convertView = itemView;
            actName = (TextView) itemView.findViewById(R.id.tv_actname);
            //actDescription = (TextView) itemView.findViewById(R.id.tv_actdescription);
            actImage = (ImageView) itemView.findViewById(R.id.iv_actimage);
        }
    }
}